package main

import (
	"fmt"
)

func main() {
	username := "root"
	password := "123456"
	var balance float64 = 1000
	entry := login(username, password)
	if entry {
		system(&balance)
	}
}

func login(u string, p string) bool {
	cnt := 0
	fmt.Println("欢迎进入登录系统")
	for {
		var name, passwd string
		fmt.Println("请输入用户名和密码")
		fmt.Scan(&name)
		fmt.Scan(&passwd)
		if name == u && passwd == p {
			fmt.Println("登录成功！")
			return true
		}
		cnt++
		if cnt >= 3 {
			fmt.Println("失败次数太多，无法继续登录")
			break
		}
		fmt.Println("登录失败，请重试！")
		fmt.Printf("你还有%d次机会\n", 3-cnt)
	}
	return false
}

func system(bal *float64) {
	var choice string
	var money float64
	fmt.Println("欢迎进入存取款系统")
finish:
	for {
		fmt.Println("请输入选项：")
		fmt.Println("1.存款")
		fmt.Println("2.取款")
		fmt.Println("3.查询余额")
		fmt.Println("4.退出系统")
		fmt.Scan(&choice)
		if choice != "1" && choice != "2" && choice != "3" && choice != "4" {
			fmt.Println("没有这个选项")
		}
		switch choice {
		case "1":
			for {
				fmt.Println("请输入您要存入的金额:")
				fmt.Scan(&money)
				if money < 0 {
					fmt.Println("金额不得小于零，请重试")
					continue
				}
				*bal += money
				fmt.Printf("已成功存入%.2f元\n", money)
				break
			}
		case "2":
			for {
				fmt.Println("请输入您要取出的金额:")
				fmt.Scan(&money)
				if money < 0 || money > *bal {
					fmt.Println("金额不得小于零，大于余额，请重试")
					continue
				}
				*bal -= money
				fmt.Printf("已成功取出%.2f元\n", money)
				break
			}
		case "3":
			fmt.Printf("该账户的余额是%.2f元\n", *bal)
		case "4":
			fmt.Println("已成功退出系统")
			break finish
		}
	}

}
